/**
 * @overview 创建面
 * @author dengqiang
 * @update  2022.11.15
 * @params option参数
 * position: 经纬度 (array)
 * centerPos: 中心点经纬度 (object)
 * label: {
 *  text: 标注名称 (string)
 * }
 * polygon: {
 *  perPositionHeight: 是否是用指定的高度 (boolean)
 *  fillColor: 填充色 (color, rgba)
 * }
 * polyline: {
 *  color: 边框线颜色 (color, rgba)
 *  width: 宽度 (number)
 *  type: 线段类型 (string) "dashed"(虚线), "solid"(实线)
 *  dashLength: 虚线线段长度 (number)
 * }
 */
class GWarPolygon {
  constructor() {
    window.GWarPolygonArray = []
  }

  //新增单个面
  add(paramObj) {
    const { name, option, parentName } = paramObj

    //原生方式
    const entity = {
      position: option.centerpos,
      label: {
        text: option.label.text || '',
        font: option.label.font || '18px san-serif',
        pixelOffset: option.label.labelOffset || new Cesium.Cartesian2(0, -10),
        distanceDisplayCondition:
          option.label.distanceDisplayCondition || new Cesium.DistanceDisplayCondition(0, 6000),
        disableDepthTestDistance: 10000,
        showBackground: true, //是否显示背景
        backgroundColor: option.label.backgroundColor || new Cesium.Color(0.165, 0.165, 0.165, 0.8), //背景颜色
        backgroundPadding: option.label.backgroundPadding || new Cesium.Cartesian2(7, 5), //背景边距
        fillColor:
          (option.label.fillColor && Cesium.Color.fromCssColorString(option.label.fillColor)) ||
          Cesium.Color.WHITE, //填充色
        outlineColor: option.label.outlineColor || new Cesium.Color(0, 1, 1, 0), //外线颜色
        outlineWidth: option.label.outlineWidth || 1.0, //外线宽
      },
      polygon: {
        hierarchy: new Cesium.CallbackProperty(function (time, result) {
          return new Cesium.PolygonHierarchy(option.position, null)
        }, false),
        perPositionHeight: (option.polygon && option.polygon.perPositionHeight) || true, // 指定是否使用每个位置的高度
        material:
          (option?.polygon?.fillColor &&
            Cesium.Color.fromCssColorString(option.polygon.fillColor)) ||
          Cesium.Color.WHITE.withAlpha(0.01), //填充色
        stRotation: -0.78,
        distanceDisplayCondition:
          (option.polygon && option.polygon.distanceDisplayCondition) ||
          Cesium.DistanceDisplayCondition(10, 6000),
      },
    }

    //禁停区
    if (option.material) {
      entity.polygon.material = option.material
    }

    //添加边框
    if (option.polyline) {
      entity.polyline = {
        positions: [...option.position, option.position[0]],
        width: option.polyline.width || 5,
        material:
          (option.polyline.color && Cesium.Color.fromCssColorString(option.polyline.color)) ||
          Cesium.Color.RED,
        clampToGround: option.polyline.clampToGround || false,
      }

      //边框线为虚线
      if (option.polyline.type === 'dashed') {
        entity.polyline.material = new Cesium.PolylineDashMaterialProperty({
          color:
            (option.polyline.color && Cesium.Color.fromCssColorString(option.polyline.color)) ||
            Cesium.Color.RED,
          dashLength: option.polyline.dashLength || 30, //短划线长度
        })
      }
    }

    const polygon = window.map3DControl.viewer.entities.add(entity)

    polygon.attribute = option.attribute || {} //支持添加自定义属性
    polygon.attribute.name = name
    polygon.attribute.parentName = parentName
    polygon.attribute.position = option.position.map((item) =>
      window.cesiumMapTool.car3ToWGS84Two(item)
    )
    polygon.attribute.zxdjdwd = window.cesiumMapTool.car3ToWGS84Two(option.centerpos)

    const polygonObject = {
      name: name,
      parentName: parentName,
      polygon: polygon,
    }
    window.GWarPolygonArray.push(polygonObject)
    return polygon
  }

  //根据name移除单个面
  removeByName(name) {
    window.GWarPolygonArray.forEach((item, index) => {
      if (item.name == name) {
        window.GWarPolygonArray.splice(index, 1)
        window.map3DControl.viewer.entities.remove(item.polygon)
      }
    })
  }

  //根据parentName移除一批面
  removeByPatName(parentName) {
    const length = window.GWarPolygonArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GWarPolygonArray[i].parentName == parentName) {
        window.map3DControl.viewer.entities.remove(window.GWarPolygonArray[i].polygon)
        window.GWarPolygonArray.splice(i, 1)
      }
    }
  }

  //移除所有面
  removeAll() {
    window.GWarPolygonArray.forEach((item) => {
      window.map3DControl.viewer.entities.remove(item.polygon)
    })
    window.GWarPolygonArray = []
  }

  //根据name控制单个面的显隐
  setVisibleByName(name, boolean) {
    window.GWarPolygonArray.forEach((item) => {
      if (item.name == name) {
        item.polygon.show = boolean
      }
    })
  }

  //根据parentName控制一批面的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GWarPolygonArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.polygon.show = boolean
      }
    })
  }

  //根据name设置标注名称
  setLabelText(name, text) {
    window.GWarPolygonArray.forEach((item) => {
      if (item.name == name) {
        item.polygon.label.text.setValue(text)
      }
    })
  }

  //根据name设置标注的位置
  setLabelPosition(name, newPosition) {
    window.GWarPolygonArray.forEach((item) => {
      if (item.name == name) {
        item.polygon.position = newPosition

        //动态修改线的位置
        if (item.polygon.polyline) {
          const position = [...item.polygon.polygon.hierarchy.getValue().positions]
          position.push(position[0])
          item.polygon.polyline.positions.setValue(position)
        }
      }
    })
  }

  //根据name设置面的位置
  setPosition(name, newPosition) {
    window.GWarPolygonArray.forEach((item) => {
      if (item.name == name) {
        item.polygon.position = window.cesiumMapTool.getPolygonCenter(newPosition) //标注位置

        if (item.polygon.polygon) {
          item.polygon.polygon.hierarchy = new Cesium.CallbackProperty(function (time, result) {
            return new Cesium.PolygonHierarchy(newPosition, null)
          }, false) //面位置
        }

        //线的位置
        if (item.polygon.polyline) {
          const position = [...item.polygon.polygon.hierarchy.getValue().positions]
          position.push(position[0])
          item.polygon.polyline.positions.setValue(position)
        }
      }
    })
  }

  //根据name设置选中效果
  setActive(name, boolean, time = 100) {
    window.GWarPolygonArray.forEach((item) => {
      if (item.name == name) {
        if (item.polygon.polygon.material.color) {
          //如果面没有填充色 线添加选中效果
          if (item.polygon.polygon.material.color.getValue().alpha === 0.01) {
            window.cesiumMapTool.requestAnimationFrameTwo(
              item.polygon.polyline.material.color,
              boolean,
              200,
              item.polygon.polyline
            )
          } else {
            window.cesiumMapTool.requestAnimationFrameTwo(
              item.polygon.polygon.material.color,
              boolean,
              time,
              item.polygon.polygon
            )
          }
        }
        //禁停区
        else if (item.polygon.polygon.material.oddColor) {
          window.cesiumMapTool.requestAnimationFrameTwo(
            item.polygon.polygon.material.oddColor,
            boolean,
            time,
            item.polygon.polygon
          )
        }
      }
    })
  }
}
export default GWarPolygon
